<html>
<head><meta charset="utf-8"><title>Implementing a Sink and async locks · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html">Implementing a Sink and async locks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234381495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234381495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234381495">(Apr 13 2021 at 18:57)</a>:</h4>
<p>Talking of async locks, I found that <code>futures::Sink</code> implementation is quite challenging if the implementation needs to access resources behind locks. In my implementation, so far I had to keep the <a href="https://gitlab.freedesktop.org/dbus/zbus/-/blob/984428d42098e7ffefdfe67bef36c11a1fbdde8d/zbus/src/azync/connection.rs#L827">mutex guard throughout the Sink lifetime</a>, which isn't ideal of course. The main issue here is the separation of <code>poll_ready</code> and <code>start_send</code> methods combined with the fact that <code>start_send</code> doesn't take a context argument and doesn't return a <code>Poll</code>, so it can't just yield and hence I'll have to keep the mutex guard between these calls at least.</p>



<a name="234381959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234381959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sean McArthur <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234381959">(Apr 13 2021 at 19:00)</a>:</h4>
<p>Yea, it can be difficult when <code>poll_ready</code> needs to reserve a slot, since there's no real way to disarm it if it turns out you didn't need it. As an example, tokio's <code>mpsc::Sender</code> ditched <code>poll_ready</code> for a <code>reserve</code> method instead: <a href="https://docs.rs/tokio/1.5.0/tokio/sync/mpsc/struct.Sender.html#method.reserve">https://docs.rs/tokio/1.5.0/tokio/sync/mpsc/struct.Sender.html#method.reserve</a></p>



<a name="234382156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234382156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234382156">(Apr 13 2021 at 19:01)</a>:</h4>
<p>Are you sure you need an async lock over a blocking lock here?</p>



<a name="234382625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234382625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234382625">(Apr 13 2021 at 19:04)</a>:</h4>
<p>Good question. I definitely needed it when I started off but it might be that I don't anymore.</p>



<a name="234382706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234382706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234382706">(Apr 13 2021 at 19:04)</a>:</h4>
<p>Unless you use it outside that file you sent us in a way that prevents it, then you can use a blocking lock here.</p>



<a name="234383562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234383562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234383562">(Apr 13 2021 at 19:09)</a>:</h4>
<p>well I do need to use it <a href="https://gitlab.freedesktop.org/dbus/zbus/-/blob/984428d42098e7ffefdfe67bef36c11a1fbdde8d/zbus/src/azync/connection.rs#L498">here</a> from an async method, just that currently it's indirectly through the Sink impl.</p>



<a name="234383727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234383727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234383727">(Apr 13 2021 at 19:10)</a>:</h4>
<p>Sure, that's no problem.</p>



<a name="234383792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234383792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234383792">(Apr 13 2021 at 19:10)</a>:</h4>
<p>I was mostly asking about uses that are <em>not</em> through the <code>Sink</code> impl.</p>



<a name="234384154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384154">(Apr 13 2021 at 19:13)</a>:</h4>
<p>to be able to access the resource from my async method, I'll need to do it through async lock, whether I use it through the Sink or not, or am I missing something?</p>



<a name="234384347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384347">(Apr 13 2021 at 19:14)</a>:</h4>
<p>Just because you're in an async context doesn't mean you can't use a blocking lock.</p>



<a name="234384398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384398">(Apr 13 2021 at 19:15)</a>:</h4>
<p>The only requirement is that you must release it again before you yield to the executor.</p>



<a name="234384443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384443">(Apr 13 2021 at 19:15)</a>:</h4>
<p>i.e. you can lock it in <code>poll_ready</code> or <code>start_send</code> and release the lock before you return from the method</p>



<a name="234384450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384450">(Apr 13 2021 at 19:15)</a>:</h4>
<p>and then it's all good</p>



<a name="234384583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384583">(Apr 13 2021 at 19:16)</a>:</h4>
<p>The only thing that an async lock lets you do that you can't with a blocking lock is keep it locked while doing an <code>.await</code></p>



<a name="234384702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234384702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234384702">(Apr 13 2021 at 19:17)</a>:</h4>
<p>(which you may want if you expect the lock to be held for anything more than a very short time)</p>



<a name="234399102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234399102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234399102">(Apr 13 2021 at 20:56)</a>:</h4>
<p>yeah but you can't block in async context and hence why async lock usage is justified here IMO</p>



<a name="234399170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234399170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234399170">(Apr 13 2021 at 20:56)</a>:</h4>
<p>can't -&gt; shouldn't</p>



<a name="234399216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234399216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234399216">(Apr 13 2021 at 20:57)</a>:</h4>
<p>Using a blocking lock is not necessarily the same as blocking the thread.</p>



<a name="234399297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234399297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234399297">(Apr 13 2021 at 20:57)</a>:</h4>
<p>See e.g. <a href="https://docs.rs/tokio/1/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use">https://docs.rs/tokio/1/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use</a></p>



<a name="234399495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234399495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234399495">(Apr 13 2021 at 20:59)</a>:</h4>
<p>Or as I said it at the end of <a href="https://ryhl.io/blog/async-what-is-blocking/">Async: What is blocking?</a>,  if you block for a short time, is it really blocking?</p>



<a name="234400456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234400456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234400456">(Apr 13 2021 at 21:02)</a>:</h4>
<p>yeah I read that but only till:</p>
<blockquote>
<p>The primary use case for the async mutex is to provide shared mutable access to IO resources such as a database connection. If the value behind the mutex is just data, it’s usually appropriate to use a blocking mutex</p>
</blockquote>
<p>Since my resource here is a socket, async lock seemed the right thing here. The message sending operation can block here.</p>



<a name="234400723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234400723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234400723">(Apr 13 2021 at 21:03)</a>:</h4>
<p>Right. When it comes to IO operations, you must drop down to poll methods to use blocking locks, but you <em>are</em> doing that by virtue of your <code>Sink</code> impl.</p>



<a name="234400869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234400869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234400869">(Apr 13 2021 at 21:04)</a>:</h4>
<p>The async lock is necessary to keep the IO resource locked across the full duration of the IO operation. The blocking lock would only keep it locked during each call to a poll method.</p>



<a name="234401402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234401402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234401402">(Apr 13 2021 at 21:06)</a>:</h4>
<p>So the difference between the two approaches shows up if multiple tasks acquire a <code>Sink</code> to the same IO resource and try to use it at the same time.</p>



<a name="234401728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234401728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234401728">(Apr 13 2021 at 21:07)</a>:</h4>
<p>If that does happen, you might need the async lock for correctness in that you may end up with incorrect code if multiple tasks access the resource inside the <code>Sink</code> concurrently, but if that doesn't happen, then you're just using a much more expensive lock than you need to.</p>



<a name="234401758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234401758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234401758">(Apr 13 2021 at 21:07)</a>:</h4>
<p>Ah, missed this one:</p>
<blockquote>
<p>i.e. you can lock it in poll_ready or start_send and release the lock before you return from the method</p>
</blockquote>
<p>Right, that makes sense. Many thanks. I'll try that.</p>



<a name="234402569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234402569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234402569">(Apr 13 2021 at 21:11)</a>:</h4>
<blockquote>
<p>So the difference between the two approaches shows up if multiple tasks acquire a Sink to the same IO resource and try to use it at the same time.</p>
</blockquote>
<p>Well, yeah. That can easily happen, can't it? Multiple tasks calling the <a href="https://gitlab.freedesktop.org/dbus/zbus/-/blob/984428d42098e7ffefdfe67bef36c11a1fbdde8d/zbus/src/azync/connection.rs#L412">sink</a> method.</p>



<a name="234402703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234402703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234402703">(Apr 13 2021 at 21:11)</a>:</h4>
<p>sorry, i think for some reason I'm getting message after I send out my reply here. <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="234403120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234403120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234403120">(Apr 13 2021 at 21:13)</a>:</h4>
<p>Hmm, since your <code>poll_ready</code> is a no-op, I think the code is correct even with the blocking lock.</p>



<a name="234403353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234403353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234403353">(Apr 13 2021 at 21:14)</a>:</h4>
<p>Hmm, but there's the question of <code>flush</code>.</p>



<a name="234403562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234403562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234403562">(Apr 13 2021 at 21:15)</a>:</h4>
<p>Okay, you probably need the async lock to ensure correctness of <code>poll_flush</code>.</p>



<a name="234403606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234403606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234403606">(Apr 13 2021 at 21:15)</a>:</h4>
<p>Because two tasks trying to flush at the same time can get their wakers mixed up.</p>



<a name="234403917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234403917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234403917">(Apr 13 2021 at 21:18)</a>:</h4>
<p>If we go back to your original issue.. If you are to use an async lock (and not lock it before-hand), then you are supposed to lock it in <code>poll_ready</code> and keep it locked until the <code>start_send</code> call. Anything async that needs to happen to prepare for a send should happen in <code>poll_ready</code>.</p>



<a name="234471783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234471783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zeeshan Ali <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234471783">(Apr 14 2021 at 10:00)</a>:</h4>
<p>right but I was told that would also be wrong/bad so maybe it's more a documentation issue than anything else. <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="234513084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementing%20a%20Sink%20and%20async%20locks/near/234513084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementing.20a.20Sink.20and.20async.20locks.html#234513084">(Apr 14 2021 at 14:58)</a>:</h4>
<p>Yes, it's a documentation issue. Blocking mutexes in poll methods are perfectly fine as long as you don't get the wakers mixed up. For example <code>tokio::io::split</code> uses a mutex.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>